package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.commen.R;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.domain.SetmealDish;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.service.ISetmealDishService;
import com.itheima.reggie.service.ISetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private ISetmealDishService setmealDishService;
    @Autowired
    private ISetmealService setmealService;

    //    新增套餐接口
    @PostMapping
    public R<String> addSetmeal(@RequestBody SetmealDto setmealDto) {
        setmealService.save(setmealDto);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

        for (SetmealDish setmealDish : setmealDishes) {
            Long id = setmealDto.getId();
            setmealDish.setSetmealId(id);
        }
        setmealDishService.saveBatch(setmealDishes);

        return R.success("新增套餐成功");
    }

    //    套餐分页查询
    @GetMapping("/page")
    public R page(Integer page, Integer pageSize, String name) {

        //分页构造器对象
        Page<Setmeal> pageInfo = new Page<>(page, pageSize);

        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        //添加查询条件，根据name进行like模糊查询
        queryWrapper.like(name != null, Setmeal::getName, name);
        //添加排序条件，根据更新时间降序排列
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        setmealService.page(pageInfo, queryWrapper);
        List<Setmeal> records = pageInfo.getRecords();
        long total = pageInfo.getTotal();
        Map map = new HashMap();
        map.put("total", (int) total);
        map.put("records", records);
        return R.success(map);
    }

    //    删除套餐
    @DeleteMapping
    public R deleteByIds(@RequestParam List<Long> ids) {
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.in(Setmeal::getId, ids).eq(Setmeal::getStatus, 1);
        int count = setmealService.count(qw);
        if (count != 0) {
            return R.error("套餐处于售卖状态,不允许删除");
        }
        LambdaQueryWrapper<Setmeal> qw1 = new LambdaQueryWrapper<>();
        qw1.in(Setmeal::getId, ids);
        setmealDishService.removeByIds(ids);

        setmealService.remove(qw1);

        return R.success("删除成功");
    }

    //    起售/停售套餐
    @PostMapping("/status/{status}")
    public R updateStatus(@PathVariable Integer status,
                          @RequestParam List<Long> ids) {
        LambdaUpdateWrapper<Setmeal> qw = new LambdaUpdateWrapper<>();
        qw.set(Setmeal::getStatus, status);
        qw.in(Setmeal::getId, ids);
        setmealService.update(qw);
        return R.success("操作成功");
    }

    //    根据套餐id查询套餐数据
    @GetMapping("{id}")
    public R getById(@PathVariable Long id) {
        Setmeal setmeal = setmealService.getById(id);
        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
        qw.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> setmealDishList = setmealDishService.list(qw);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal, setmealDto);
        setmealDto.setSetmealDishes(setmealDishList);
        return R.success(setmealDto);
    }

    @PutMapping
    public R updateSetmeal(@RequestBody SetmealDto setmealDto) {

        setmealService.updateById(setmealDto);
        setmealDishService.removeById(setmealDto.getId());
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());
        }
        setmealDishService.saveBatch(setmealDishes);
        return R.success("修改成功");
    }

    @GetMapping("/list")
    public R list(Setmeal setmeal) {

        Long categoryId = setmeal.getCategoryId();
        Integer status = setmeal.getStatus();


        LambdaQueryWrapper<Setmeal> qw=new LambdaQueryWrapper<>();
        qw.eq(Setmeal::getCategoryId,categoryId).eq(Setmeal::getStatus,status);
        qw.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = setmealService.list(qw);

        return R.success(list);

    }

}
